/**
 * 
 */
package de.oszimtcc.timetableservices;

import java.util.Calendar;
import java.util.Date;
import java.util.Random;

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Binder;
import android.os.Bundle;
import android.os.IBinder;
import android.os.Messenger;
import android.util.Log;
import android.widget.Toast;
import de.oszimtcc.framework.BaseBinder;
import de.oszimtcc.framework.BaseService;
import de.oszimtcc.framework.Logger;
import de.oszimtcc.timetable.R;
import de.oszimtcc.timetable.TimetableApplication;


/**
 * @author reuter
 *
 */
public class TimetableChangingService extends BaseService
{
	private TimetableServiceConnection myConnection;
	private BaseBinder<TimetableChangingService> myBinder;
	public NotificationHandler notificationHandler;
	NotificationManager manager = (NotificationManager)TimetableApplication.getInstance().getSystemService(Context.NOTIFICATION_SERVICE);
	private int request_code = 19051986;
	
	@Override
	public IBinder onBind(Intent intent) 
	{
		if(myBinder == null)
			myBinder = (BaseBinder<TimetableChangingService>)super.onBind(intent);
		return myBinder;
	}
	

	@Override
	public void onCreate() 
	{
	    super.onCreate();
		Thread notificationThread = new Thread(notificationService,"notificationService");
		notificationThread.start();
	}

	@Override
    public int onStartCommand(Intent intent, int flags, int startId)
    {
		myBinder = (BaseBinder<TimetableChangingService>)super.onBind(intent);
		Toast.makeText(this, "Received start id " + startId + ": " + intent, Toast.LENGTH_SHORT).show();
		SetAlarmManager();
	    return START_STICKY;
    }

	@Override
	public void onDestroy()
	{
	    super.onDestroy();
	}
	
	public BaseBinder<TimetableChangingService> GetServiceBinder()
	{
		return this.myBinder;
	}

	public void SetAlarmManager() 
    {   
		Logger.Debug("start setting AlarmManager");

		Intent intent = new Intent(this, OnAlarmReceiver.class);
		intent.putExtra("message", "Ich bin die Alarmnachricht");
		AlarmManager am = (AlarmManager)this.getSystemService(Context.ALARM_SERVICE);
			
		Calendar cal = Calendar.getInstance();
		cal.setTime(new Date());
		cal.add(Calendar.SECOND, 5);
		
		PendingIntent sender = PendingIntent.getBroadcast(this, request_code, intent, PendingIntent.FLAG_CANCEL_CURRENT);
 
		Toast.makeText(this, "Alarmmanager set to 5s", Toast.LENGTH_SHORT).show();
		am.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(), 5000, sender);
    }
	
	private Runnable notificationService = new Runnable()
	{	
		@Override
		public void run()
		{
			while(true)
			{
				Random rand = new Random();
				try
                {
	                Thread.sleep(((rand.nextInt(5)+5)*1000));
                }
                catch(InterruptedException e)
                {
                	Logger.Error("NotificationThread Exception : " + e);
                }
                //new Notifications(TimetableApplication.getInstance().getContext()).setAlarmManager();
                Logger.Debug("new Notification");
			}
		}
	};
}
